/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-22 17:04:39
 */
import { Modal, message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { curry } from 'ramda'
import { createVNode } from 'vue'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
import { DebouncedFunc } from './tools/throttle'
type Func = DebouncedFunc<(params: ObjectMap, options?: ObjectMap) => Promise<any>>
export const valueFormat = 'YYYY-MM-DD HH:mm:ss'
export const mobileReg = /^1[3-9]\d{9}$/
export const rangePickerShowTime = {
	defaultValue: [dayjs('00:00:00', 'HH:mm:ss'), dayjs('23:59:59', 'HH:mm:ss')],
}
/**
 *@desc: 数组中含有对象，判断值在不在数组的对象中
 * @param {any[]} arr
 * @param {string} key
 * @param {(string | number)} value
 * @return { boolean} 结果
 */
export function arrIsKey(arr: any[], key: string, value: string | number) {
	let result = false
	result = arr.some((item: any) => {
		if (item[key] == value) {
			return true
		}
	})
	return result
}
//提示信息优化
function notificationTips(status: 'success' | 'error', description: string) {
	message[status](description)
}
const curryTips = curry(notificationTips)
export const successTips = curryTips('success')
export const errorTips = curryTips('error')
export interface FilterOption {
	children: string
	key: string
	value: string | number
	title?: string
	label?: string
	mobile?: string
}
/** 不区分大小写 */
export function filterOptionCaseInsensitive(inputValue: string, option: FilterOption) {
	const label = option.title ?? option.label ?? option.children
	/** 手机号搜索 */
	const mobile = option.mobile?.toUpperCase().includes(inputValue.toUpperCase())
	return label.toUpperCase().includes(inputValue.toUpperCase()) || mobile
}

export const dateFormat = 'YYYY-MM-DD HH:mm'

export const defaultParams = {
	pageSize: 10,
	page: 1,
}
/**
 * 
 * @param params 删除数据
 * @param request 删除接口
 * @param callback 删除成功回调
 * @param confirmConfig 弹窗配置
 */
export function deleteData<T extends (...arg: any) => Promise<any>>(params: ObjectMap, request: T, callback?: Function, confirmConfig = {} as any) {
	Modal.confirm({
		title: confirmConfig.title ?? '是否确定删除该数据？',
		icon: createVNode(ExclamationCircleOutlined),
		onOk: async () => {
			const { success } = await request(params)
			if (success) {
				successTips('删除成功')
				callback && callback()
			}
		},
	})
}

/**
 * 将 Data URL 转换为 Blob 对象
 * @param dataURI Data URL
 * @returns 
 */
export function dataURItoBlob(dataURI: string): Blob {
	const byteString = atob(dataURI.split(',')[1]);
	const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
	const ab = new ArrayBuffer(byteString.length);
	const ia = new Uint8Array(ab);
	for (let i = 0; i < byteString.length; i++) {
		ia[i] = byteString.charCodeAt(i);
	}
	return new Blob([ab], { type: mimeString });
}